package binarysearch.搜索插入位置;

/**
 * 给定一个排序数组和一个目标值，在数组中找到目标值，并返回其索引。如果目标值不存在于数组中，返回它将会被按顺序插入的位置。
 */
public class Solution {

    public int searchInsert(int[] nums, int target) {
        return search(nums, 0, nums.length  , target);
    }

    int search(int[] nums, int start, int end, int target) {
        if (start >= end) {
            return start;
        }
        int mid = (start + end) / 2;
        if (target == nums[mid]) {
            return mid;
        } else if (target > nums[mid]) {
            return search(nums, mid + 1, end, target);
        } else {
            return search(nums, start, mid, target);
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.searchInsert(new int[]{1,2,5,6},7));
    }

}
